<?php
	/**
	 * 战绩分享
	 */
    class CombatGainsShare extends APIBase {
		
		public $tag = "CombatGainsShare";
    	public $isLogin = true;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic(){              
			
			if(!isset($this->param['deskBuildDate']) ||!isset($this->param['deskCode'])){
				$this->returnError(301,'参数错误!');
			}
			$date = $this->param['deskBuildDate'];
			$deskCode = $this->param['deskCode'];
			$playerID = $this->uid;
			//统计在同一deskBuildDate同一deskcode下玩家数据
			$UserCombatgainsdetail = $this->cache_redis->keys("hUserCombatgainsdetail:$playerID:$date:$deskCode:*");
			if(!empty($UserCombatgainsdetail)){
				$count = count($UserCombatgainsdetail);
				$result = array();
			    if ($count) {
				    foreach($UserCombatgainsdetail as $key=>$val){ 
					 $result[] = $this->cache_redis->hGetAll("hUserCombatgainsdetail:$playerID:$date:".$i);   
				    }
	            }
				$this->returnData($result);
			}else{	
			    //echo "<pre>";
				//统计在同一deskBuildDate同一deskcode下玩家数据
				$result = $this->mysql->select("customgamelog","SUM(score) as total,playerID,deskCode",array('deskBuildDate'=>$date,'deskCode'=>$deskCode),'GROUP BY playerID');
                if(empty($result)){
					$this->returnError(302,'参数错误!');
				}				
				if(!empty($result)){
					$userinfo_tal = array();
					foreach($result as $v){
							//$user = $this->mysql->select('player','id,name',array('id'=>$v['playerID']));
							$user = $this->data_redis->hMget("hu:".$v['playerID'],array('name','avater_url','id'));
							$userinfo_tal[] = array('point'=>$v['total'],'name'=>$user['name'],'playerID'=>$user['id'],'pic'=>$user['avater_url']);	
					}
                }

			    
				$row = $this->mysql->find("select gameDate FROM customgamelog where deskBuildDate=$date and deskCode=$deskCode  GROUP BY gameDate");
				//取最后一局结束时间
			    $LastGameDate = max($row)['gameDate'];
				$ownerid = '';
                for($k=0;$k < count($row);$k++){
					$gameDate = $row[$k]['gameDate'];
					$row0 = $this->mysql->find("select playerID,score,ownerid FROM customgamelog where deskBuildDate=$date and deskCode=$deskCode and gameDate = $gameDate");
					$winplayer = array();
                    foreach($row0 as $v){
						if($v['score']>0){
							$winplayer[] = $v['playerID'];
						}
						$ownerid = $v['ownerid'];
			 		}
					$data[] = $winplayer;	
				}
                $wincount = array_count_values(arrToOne($data));
				foreach($userinfo_tal as $key=>$v){
					if(isset($wincount[$v['playerID']])){
						$userinfo_tal[$key]['wincount'] = $wincount[$v['playerID']];
					}else{
						$userinfo_tal[$key]['wincount'] = 0;
					}
				}
                $detail = array('survey'=>$userinfo_tal,'LastGameDate'=>$LastGameDate,'deskCode'=>$deskCode,'ownerid'=>$ownerid);
                $this->returnData($detail);					
				
			}
		}
		
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
